﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

using WHC.Pager.Entity;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.UI
{
    public partial class FrmLogonUser : BaseForm
    {
        public FrmLogonUser()
        {
            InitializeComponent();
        }

        private void FrmCustomerArea_Load(object sender, EventArgs e)
        {
            BindData();

            this.winGridViewPager1.ProgressBar = this.toolStripProgressBar1.ProgressBar;
            this.winGridViewPager1.OnPageChanged += new EventHandler(winGridViewPager1_OnPageChanged);
            this.winGridViewPager1.OnStartExport += new EventHandler(winGridViewPager1_OnStartExport);
            this.winGridViewPager1.OnEditSelected += new EventHandler(winGridViewPager1_OnEditSelected);
            this.winGridViewPager1.OnDeleteSelected += new EventHandler(winGridViewPager1_OnDeleteSelected);
            this.winGridViewPager1.OnRefresh += new EventHandler(winGridViewPager1_OnRefresh);
            this.winGridViewPager1.OnAddNew += new EventHandler(winGridViewPager1_OnAddNew);
            this.winGridViewPager1.AppendedMenu = this.contextMenuStrip1;
        }

        private void winGridViewPager1_OnRefresh(object sender, EventArgs e)
        {
            BindData();
        }

        private void winGridViewPager1_OnDeleteSelected(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                //foreach (DataGridViewRow row in grid.SelectedRows)
                //{
                //    BLLFactory<Admin>.Instance.Delete(row.Cells["ID"].Value.ToString());
                //}
                //BindData();
            }
        }

        private void winGridViewPager1_OnEditSelected(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                foreach (DataGridViewRow row in grid.SelectedRows)
                {
                    //FrmEditAdmin dlg = new FrmEditAdmin();
                    //dlg.ID = row.Cells["ID"].Value.ToString();
                    //if (DialogResult.OK == dlg.ShowDialog())
                    //{
                    //    BindData();
                    //}

                    break;
                }
            }
        }

        private void winGridViewPager1_OnAddNew(object sender, EventArgs e)
        {
            //FrmEditAdmin dlg = new FrmEditAdmin();
            //if (DialogResult.OK == dlg.ShowDialog())
            //{
            //    BindData();
            //}
        }

        private void winGridViewPager1_OnStartExport(object sender, EventArgs e)
        {
            //PagerInfo pagerInfo = new PagerInfo();
            //pagerInfo.CurrenetPageIndex = 1;
            //pagerInfo.PageSize = int.MaxValue;
            //this.winGridViewPager1.AllToExport = BLLFactory<Admin>.Instance.GetAllToDataSet(pagerInfo).Tables[0];//product.GetAllToDataSet(pagerInfo).Tables[0];
        }

        private void winGridViewPager1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void BindData()
        {
            #region 添加别名解析
            this.winGridViewPager1.AddColumnAlias("ID", "编号");
            this.winGridViewPager1.AddColumnAlias("LoginName", "登录名称");
            this.winGridViewPager1.AddColumnAlias("Password", "密码");
            this.winGridViewPager1.AddColumnAlias("ShopName", "分店名称");
            this.winGridViewPager1.AddColumnAlias("Shop_ID", "分店ID");
            this.winGridViewPager1.AddColumnAlias("RealName", "真实姓名");
            this.winGridViewPager1.AddColumnAlias("BirthDay", "出生年月");
            this.winGridViewPager1.AddColumnAlias("Telephone1", "电话1");
            this.winGridViewPager1.AddColumnAlias("Telephone2", "电话2");
            this.winGridViewPager1.AddColumnAlias("Mobile", "移动电话");
            this.winGridViewPager1.AddColumnAlias("Address", "家庭住址");
            this.winGridViewPager1.AddColumnAlias("Email", "Email地址");
            this.winGridViewPager1.AddColumnAlias("PwdQuestion", "找回密码问题");
            this.winGridViewPager1.AddColumnAlias("PwdAnswer", "找回密码答案");
            this.winGridViewPager1.AddColumnAlias("Note", "备注");
            this.winGridViewPager1.AddColumnAlias("LastUpdated", "更新日期");
            #endregion

            //product.GetAll(this.winGridViewPager1.PagerInfo);
            //DataView collection = BLLFactory<Admin>.Instance.GetAllToDataSet(this.winGridViewPager1.PagerInfo).Tables[0].DefaultView;
            //this.winGridViewPager1.DataSource = collection;
        }

        private void tsbNew_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnAddNew(null, null);
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnEditSelected(null, null);
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            winGridViewPager1_OnDeleteSelected(null, null);
        }

        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menu_ResetPass_Click(object sender, EventArgs e)
        {
            DataGridView grid = this.winGridViewPager1.dataGridView1;
            if (grid != null)
            {
                if (MessageUtil.ShowYesNoAndWarning("您确定重置选定人员的密码么，重置后密码为空!") == DialogResult.Yes)
                {
                    foreach (DataGridViewRow row in grid.SelectedRows)
                    {
                        //string ID = row.Cells["ID"].Value.ToString();
                        //string loginName = row.Cells["LoginName"].Value.ToString();
                        //BLLFactory<Admin>.Instance.ResetPassword(loginName);
                    }

                    BindData();
                }
            }
        }
    }
}
